package chariott.JUnit;

import java.util.ArrayList;
import java.util.Arrays;

import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;

import chariott.testing.RunItem;
import chariott.testing.TestCase;

public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		test myTest = new test();

		RunItem testcase3 = new TestCase("name3", 3);
		RunItem testcase2 = new TestCase("name2", 2);
		RunItem testcase1 = new TestCase("name1", 1);

		ArrayList<RunItem> myList = new ArrayList();
		myList.add(testcase1);
		myList.add(testcase2);
		myList.add(testcase3);

		System.out.println(myTest.leastUsedAlgorithm(myList, 2));

	}

	public MultiMap leastUsedAlgorithm(ArrayList<RunItem> itemsToRun,
			int numOfNodes) {

		MultiMap partitioningMap = new MultiHashMap();
		double[] nodeWeights = new double[numOfNodes];
		// initialize node wights to include 0 weights
		Arrays.fill(nodeWeights, 0);

		for (int i = 0; i < itemsToRun.size(); i++) {
			if (i < numOfNodes) {
				nodeWeights[i] += itemsToRun.get(i).getWeight();
				partitioningMap.put(i, itemsToRun.get(i));
			} else {
				double smallestNodeWeight = nodeWeights[0];
				int smallestNodeWeightIndex = 0;

				// we find the least used node in the array
				for (int j = 1; j < numOfNodes; j++)
					if (nodeWeights[j] < smallestNodeWeight)
						smallestNodeWeightIndex = j;

				// add the item with the smallest weight to the map
				partitioningMap.put(smallestNodeWeightIndex, itemsToRun.get(i));
				// increment the node weights by the weight of the item added.
				nodeWeights[smallestNodeWeightIndex] = +itemsToRun.get(i)
						.getWeight();
			}
		}
		return partitioningMap;
	}

}
